import AuthConstants from '../constants/authentication';

const initialState = {
    isLoggingIn: false,
    loginRequired: false,
    isRegister: false,
    inRegister: false,
    showSearch: false,
    submitting: false
};

const authentication = (state = initialState, action) => {
    switch (action.type) {

        case AuthConstants.REGISTER_REQUEST:
        {
            return {
              ...state,
              isRegister: true,
              registerError: false,
              submitting: true
            }
        }

        case AuthConstants.REGISTER_ERROR:
        {
            return {
              ...state,
              isRegister: false,
              registerError: action.error,
              submitting: false
            }
        }

        case AuthConstants.REGISTER_SUCCESS:
        {
            return {
              ...state,
              isRegister: true,
              submitting: false
            }
        }

        case AuthConstants.LOG_IN_PROMPT:
        {
            return {
                ...state,
                loginRequired: true,
            };
        }

        case AuthConstants.LOG_IN_REQUEST:
        {
            return {
                ...state,
                isLoggingIn: true,
                loginError: false
            };
        }

        case AuthConstants.LOG_IN_SUCCESS:
        {
            return {
                ...state,
                isLoggingIn: false,
                loginRequired: false,
                user: action.payload.user
            };
        }

        case AuthConstants.LOG_IN_ERROR:
        {
            return {
                ...state,
                isLoggingIn: false,
                loginError: action.error
            };
        }

        case AuthConstants.LOG_OUT:
        {
            return {
                ...state,
                isLoggingIn: false,
                user: null,
                loginRequired: true
            };
        }

        default:
        {
            return state;
        }
    }
};

export default authentication;
